package com.ht.pms.core.service.email;


import com.ht.pms.core.dto.send.Email;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * @Description:
 * @Date:2017/6/15 0015 17:17
 * @Author:lxq
 */
@Component
public class SendEmail {

    private static final Logger LOG = LoggerFactory.getLogger(SendEmail.class);
    public  void sendEmail(Email email) {

        Properties props = new Properties();

        props.setProperty("mail.transport.protocol",email.getEmailParam());//规范规定的参数
        props.setProperty("mail.host", email.getEmailHost());//邮箱主机
        props.setProperty("mail.smtp.auth",email.getEmailAuth());//请求认证，不认证有可能发不出去邮件。

        Session session = Session.getInstance(props);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(email.getEmailFrom()));  //这里可以根据你的实际情况更改邮箱号
            message.setRecipients( Message.RecipientType.TO, email.getEmailTo());
            message.setSubject(email.getEmailSubject());
            message.setContent(email.getEmailContent(),email.getEmailContentclass());
            message.saveChanges();
//            message.setHeader("Disposition-Notification-To", email.getEmailFrom());
            Transport ts = session.getTransport();
            ts.connect(email.getEmailUsername(),email.getEmailPwd());
            ts.sendMessage(message, message.getAllRecipients());
            ts.close();
        } catch (AddressException e) {
            LOG.error("发送邮件错误"+ e.getMessage());
        } catch (NoSuchProviderException e) {
            LOG.error("发送邮件错误"+ e.getMessage());
        } catch (MessagingException e) {
            LOG.error("发送邮件错误"+ e.getMessage());
        }catch (Exception e){
            LOG.error("==========>>>>>>>>>发送邮件错误{e}",e);
        }
//        LoggerFactoryUtil.biz("-----------发送邮件结束");
    }





}
